package com.woniu.server.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.server.dto.ApplicationProjectDto;
import com.woniu.server.pojo.ApplicationProduct;
import com.woniu.server.pojo.ApprovalRecord;
import com.woniu.server.service.ApplicationProductService;
import com.woniu.server.mapper.ApplicationProductMapper;
import com.woniu.server.service.ApprovalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author a
* @description 针对表【application_product(申请产品详细表)】的数据库操作Service实现
* @createDate 2024-11-23 17:46:19
*/
@Service
public class ApplicationProductServiceImpl extends ServiceImpl<ApplicationProductMapper, ApplicationProduct>
    implements ApplicationProductService{
    @Autowired
    private ApprovalRecordService approvalRecordService;


    /**
     * 根据申请表的编号来生成对应的申请产品详细表
     * @Author hht
     * @Date Create by 2024/11/24 12:54
     */
    @Transactional
    @Override
    public void addApplicationProductByApplicationCode(String projectApplicationCode,
                                                       String applicationCode,
                                                       List<ApplicationProjectDto> projectDtoList,
                                                       String processInstanceId) {
        for (ApplicationProjectDto applicationProjectDto : projectDtoList) {
            //添加申请产品详细表
            ApplicationProduct applicationProduct = new ApplicationProduct();
            String applicationProductCode = String.valueOf(IdUtil.getSnowflake(1, 1).nextId());
            applicationProduct.setApplicationProductCode(applicationProductCode);
            applicationProduct.setProjectApplicationCode(projectApplicationCode);
            applicationProduct.setStatus(1);
            applicationProduct.setProjectName(applicationProjectDto.getProjectName());
            applicationProduct.setProjectType(applicationProjectDto.getProjectType());
            applicationProduct.setTargetCustomer(applicationProjectDto.getTargetCustomer());
            applicationProduct.setTargetMarket(applicationProjectDto.getTargetMarket());
            applicationProduct.setProductName(applicationProjectDto.getProductName());
            applicationProduct.setProjectedCost(applicationProjectDto.getProjectedCost());
            applicationProduct.setSellPrice(applicationProjectDto.getSellPrice());
            applicationProduct.setProjectedProfit(applicationProjectDto.getProjectedProfit());
            applicationProduct.setProductMaterial(applicationProjectDto.getProductMaterial());
            applicationProduct.setProductDescription(applicationProjectDto.getProductDescription());
            applicationProduct.setCreatedBy(applicationCode);
            save(applicationProduct);
            //添加申请表的流程表
            ApprovalRecord approvalRecord = new ApprovalRecord();
            String approvalRecordCode = String.valueOf(IdUtil.getSnowflake(1, 1).nextId());
            approvalRecord.setApprovalRecordCode(approvalRecordCode);
            approvalRecord.setApplicationProductCode(applicationProductCode);
            approvalRecord.setStatus(1);
            approvalRecord.setStepOrder(1);
            approvalRecord.setApprovalResult(0);
            approvalRecord.setProcessInstanceId(processInstanceId);
            approvalRecord.setCreatedBy(applicationCode);
            approvalRecordService.save(approvalRecord);
        }
    }

    @Override
    public List<ApplicationProduct> queryApplicationProductByApplicationCode(String projectApplicationCode) {
        return lambdaQuery().eq(ApplicationProduct::getProjectApplicationCode, projectApplicationCode).list();
    }

    @Override
    public ApplicationProduct queryApplicationProductByApplicationProductCode(String applicationProductCode) {
        ApplicationProduct one = lambdaQuery().eq(ApplicationProduct::getApplicationProductCode, applicationProductCode)
                .one();
        return one;
    }

    @Override
    public void updateStatusByCode(ApplicationProduct applicationProduct) {
        lambdaUpdate()
                .eq(ApplicationProduct::getApplicationProductCode, applicationProduct.getApplicationProductCode())
                .set(ApplicationProduct::getStatus, applicationProduct.getStatus())
                .update();
    }

    @Override
    public void updateProductCodeByCode(ApplicationProduct applicationProduct1) {
        lambdaUpdate()
                .eq(ApplicationProduct::getApplicationProductCode, applicationProduct1.getApplicationProductCode())
                .set(ApplicationProduct::getProductCode, applicationProduct1.getProductCode())
                .set(ApplicationProduct::getUpdatedBy, applicationProduct1.getUpdatedBy())
                .update();
    }
}




